<template>
  <div class="container">
    <div class="app-container">
      <el-button class="btn-add" size="mini" type="primary" @click="handleAdd(0)">添加权限</el-button>
      <el-table
        v-loading="loading"
        :data="tableData"
        row-key="id"
        border
        default-expand-all
        :header-cell-style="{background:'#f5f6f8',color:'#909399'}"
      >
        <el-table-column label="名称" prop="name" />
        <el-table-column label="标识" prop="code" />
        <el-table-column label="描述" prop="description" />
        <el-table-column label="操作">
          <template v-slot="{row}">
            <el-button v-if="row.type === 1" size="small" type="text" @click="handleAdd(row.id)">添加</el-button>
            <el-button size="small" type="text" @click="handleEdit(row.id)">编辑</el-button>
            <el-button size="small" type="text" @click="handleDel(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <PermissionDialog ref="pDialog" @getPermissionList="getPermissionList" />
  </div>
</template>
<script>
import { getPermissionList, deletePermission } from '@/api/permission'
import { transListToTree } from '@/utils'
import PermissionDialog from './components/PermissionDialog.vue'
export default {
  name: 'Permission',
  components: {
    PermissionDialog
  },
  data() {
    return {
      loading: false,
      tableData: []
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    async getPermissionList() {
      this.loading = true
      this.tableData = transListToTree(await getPermissionList(), 0)
      this.loading = false
    },
    // 点击添加按钮
    handleAdd(id) {
      this.$refs.pDialog.open('新增权限点', id)
    },
    // 点击编辑按钮
    handleEdit(id) {
      this.$refs.pDialog.open('编辑权限点', id)
    },
    // 删除
    handleDel(id) {
      this.$confirm('确定要删除该数据吗?', '', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await deletePermission(id)
        await this.getPermissionList()
        this.$message({
          type: 'success',
          message: '删除成功!'
        }).catch(() => {})
      })
    }
  }
}
</script>
<style>
.btn-add {
  margin: 10px;
}
</style>
